var sfs = null;
var params = null;
var resFunction = null;
var resParams = null;
function init(){
	trace("Application started");
	var config = {};
	//config.host = "127.0.0.1";
	//config.host = "192.168.34.21";
	config.host = "192.168.56.1";
	config.port = 8888;
	config.zone = "VicGame";
	config.debug = false;
	sfs = new SmartFox(config);
	// Add event listeners
	sfs.addEventListener(SFS2X.SFSEvent.CONNECTION, onConnection, this);
	sfs.addEventListener(SFS2X.SFSEvent.CONNECTION_LOST, onConnectionLost, this);
	sfs.addEventListener(SFS2X.SFSEvent.LOGIN_ERROR, onLoginError, this);
	sfs.addEventListener(SFS2X.SFSEvent.LOGIN, onLogin, this);
	sfs.addEventListener(SFS2X.SFSEvent.LOGOUT, onLogout, this);
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_JOIN_ERROR, onRoomJoinError, this);
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_JOIN, onRoomJoin, this);
	sfs.addEventListener(SFS2X.SFSEvent.PUBLIC_MESSAGE, onPublicMessage, this);
	sfs.addEventListener(SFS2X.SFSEvent.USER_ENTER_ROOM, onUserEnterRoom, this);
	sfs.addEventListener(SFS2X.SFSEvent.USER_EXIT_ROOM, onUserExitRoom, this);
	sfs.addEventListener(SFS2X.SFSEvent.USER_VARIABLES_UPDATE, onUserVariablesUpdate, this);
	sfs.addEventListener(SFS2X.SFSEvent.USER_COUNT_CHANGE, onUserCountChange, this);
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_REMOVE, onRoomRemove, this);
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_ADD, onRoomAdd, this);
	sfs.addEventListener(SFS2X.SFSEvent.INVITATION, onInvitation, this);
	sfs.addEventListener(SFS2X.SFSEvent.EXTENSION_RESPONSE, onExtensionResponse, this);
	// Show LOGIN view
	setView("login", true);
}
//------------------------------------
// SFS EVENT HANDLERS
//------------------------------------
/*
function onConnection(event){
	
}
function onConnectionLost(event){

}
function onLoginError(event){

}
function onLogin(event){

}
function onLogout(event){

}
function onRoomJoinError(event){

}
function onRoomJoin(event){

}
function onPublicMessage(event){

}
function onUserEnterRoom(event){

}
function onUserExitRoom(event){

}
function onUserVariablesUpdate(event){

}
function onUserCountChange(event){

}
function onRoomRemove(event){
	
}
function onRoomAdd(event){

}
function onInvitation(event){

}
*/
function onExtensionResponse(evtParams){
	console.log(evtParams);
	resFunction = evtParams.cmd;
	resParams = evtParams.params;
	executeFunctionByName(resFunction, window, resParams);
}
//------------------------------------
// SEND AND RECEIVE
//------------------------------------
function send(cmd, params){
	return sfs.send(new SFS2X.Requests.System.ExtensionRequest(cmd, params));
}
//------------------------------------
// UTILITIES
//------------------------------------
function trace(txt, showAlert){
	console.log(txt);
	if (showAlert)
		alert(txt);
}
function executeFunctionByName(functionName, context , args ) {
    var args = Array.prototype.slice.call(arguments, 2);
    var namespaces = functionName.split(".");
    var func = namespaces.pop();
    for (var i = 0; i < namespaces.length; i++) {
        context = context[namespaces[i]];
    }
    return context[func].apply(context, args);
}
//------------------------------------
// TEST
//------------------------------------
/*
var params = {};
params.n1 = 26;
params.n2 = 16;

sfs.send(new SFS2X.Requests.System.ExtensionRequest("add", params));
*/